<?php

class lenta extends Module {
	
	public function __construct () {
		parent::__construct (__CLASS__);
		
		$this->extend ('__custom_lenta');
	}
	
	public function __default () {
		$typesController = TypesController::getInstance ();
		$oTypeConnection = $typesController->getTypeByName ('explay_types_connection');
		$objectsController = ObjectsController::getInstance ();
		
		$sel = new Selection;
		$sel->addType ($typesController->getTypeByName ('friend_log_type'));
		$sel->addSelectedProperty ('type_id');
		$sel->addSelectedProperty ('connection_id');
		
		$res = $sel->getList ();
		
		$aFollowsConnections = array ();
		
		foreach ($res as $item) {
			$aTypesIds[] = $item['type_id'];
			$aFollowsConnections[$item['type_id']] = $objectsController->getObject ($oTypeConnection, $item['connection_id']);
		}
		
		if (!count ($aTypesIds)) {
			throw new CoreException ('Бля, не добавлено ни одного типа лога!!!');
		}
		
		$sel = new Selection;
		
		foreach ($aTypesIds as $typeId) {
			$sel->addType ($typesController->getTypeById ($typeId));
		}
		
		$sel->addFollowFilter ($typesController->getTypeByName ('friend'), $aFollowsConnections, 'user_id', ExplaySession::getUserId ());
		
		$sel->setOrder ('date', false);
		
		$currentPage = getRequest ('page', 'int');
		if ($currentPage > 0) {
			$currentPage--;
		}
		
		$onPage = (int) Regedit::getInstance()->get ('//settings/modules/lents/objects_on_page');
		
		if ($onPage == 0) {
			$onPage = 40; 
		}
		$sel->addLimit ($currentPage * $onPage, $onPage);
		
		$aObjects = $objectsController->getObjectsList ($sel->getList ());
		
		$response = new ModuleResponse;
		$response->setTemplate ('lenta/index');
		
		foreach ($aObjects as $object) {
			$response->addObject ($object);
		}
		
		$response->addObject (new Array2XML ('pages', array (
			'onpage' => $onPage,
			'total' => $sel->getTotalCount (),
			'page' => ++$currentPage
		)));
		
		return $response;
	}
	
}